#|
Copyright 2004,2006 Vladimir Prus
Copyright 2018 Rene Rivera
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.txt or https://www.bfgroup.xyz/b2/LICENSE.txt)
|#

#|
Doc generation works in two modes: standalone embedded and website.

For the standalone we just generate the local html for inclusion in a release
archive. This is the default when no arguments are given.

For the website we clone the website branch locally, generatate the docs in
the website tree, commit, and push. The default for those arguments is to
build as if we are doing "develop" branch docs.

b2 --website-dir=website --website-doc-dir=manual/develop website

Building documentation has only been tested on Posix systems and requires:

* Ruby
* Python 2
* gem install asciidoctor
* gem install pygments.rb
* pip install --user Pygments
* pip install --user https://github.com/bfgroup/jam_pygments/archive/master.zip
|#

import asciidoctor ;
import modules ;
import path ;

project b2doc
    ;

path-constant PYGMENTS_DIR : src/pygments ;
path-constant STYLES_DIR : src/styles ;

doc-dir = [ MATCH "--doc-dir=(.*)" : [ modules.peek : ARGV ] ] ;
doc-dir ?= html ;

website = [ MATCH "(website)" : [ modules.peek : ARGV ] ] ;
website-dir = [ MATCH "--website-dir=(.*)" : [ modules.peek : ARGV ] ] ;
website-dir ?= website ;
website-doc-dir = [ MATCH "--website-doc-dir=(.*)" : [ modules.peek : ARGV ] ] ;
website-doc-dir ?= manual/develop ;
if $(website)
{
    doc-dir = $(website-dir)/$(website-doc-dir) ;
}
# ECHO $(website) .. $(website-dir) .. $(website-doc-dir) .. $(doc-dir) ;

html index : src/standalone.adoc :
    :
        # <flags>--require=$(PYGMENTS_DIR)/pygments_init.rb
        <flags>--trace
        <flags>--verbose
        # <dependency>$(PYGMENTS_DIR)/pygments_init.rb
        # <flags>"-a stylesheet=amber.css"
        # <flags>"-a stylesdir=$(STYLES_DIR)"
        # <asciidoctor-attribute>linkcss
    ;
explicit index ;

install html : index : <location>$(doc-dir) <dependency>website-html ;
explicit html ;

make index.html : : @make_redir_html : <location>.. ;
REDIR_HTML = "
<html><head><meta http-equiv=\"refresh\" content=\"0; URL=doc/html/index.html\"></head><body>
Automatic redirection failed, please go to <a href=\"doc/html/index.html\">doc/html/index.html</a>.
<!-- Distributed under the Boost Software License, Version 1.0. \(See accompanying file LICENSE.txt or copy at https://www.bfgroup.xyz/b2/LICENSE.txt\) -->
</body></html>
" ;
actions make_redir_html
{
    echo @($(<):O=F:E=$(REDIR_HTML))
}
explicit index.html ;

alias standalone-html : html ;

if $(website)
{
    make website-checkout : : @website-checkout ;
    make website-publish : html : @website-publish ;
    alias website-html : website-checkout ;
    always website-checkout ;
    always website-publish ;
}
else
{
    alias website-checkout ;
    alias website-publish ;
    alias website-html ;
}
alias website : website-publish ;
explicit website-checkout ;
explicit website-publish ;
explicit website-html ;
explicit website ;

###############################################################################

actions website-checkout
{
    rm -rf "$(website-dir)"
    git clone --verbose --branch gh-pages --depth 1 "https://${GH_TOKEN}github.com/bfgroup/b2.git" "$(website-dir)" || exit 1
    CD=${PWD}
    cd "$(website-dir)"
    git rm --ignore-unmatch -r "$(website-doc-dir)" || exit 1
    mkdir -p "$(website-doc-dir)" || exit 1
    cd ${CD}
    echo "done" > "$(<)"
}

actions website-publish
{
    CD=${PWD}
    cd "$(website-dir)/$(website-doc-dir)"
    git config user.email "b2-bot"
    git config user.name "b2-bot"
    git add --verbose . || exit 1
    git commit -m "Update user manual."
    git push || exit 1
    cd ${CD}
    rm -rf "$(website-dir)"
    echo "done" > "$(<)"
}

###############################################################################

alias boostdoc ;
explicit boostdoc ;

alias boostrelease : standalone-html index.html ;
explicit boostrelease ;
